home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / eaccess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  4.2 KB  |  175 lines

  1. /* eaccess -- check if effective user id can access file
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie and Torbjorn Granlund */
  19.  
  20. /*
  21.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  22.  *
  23.  * To this port, the same copying conditions apply as to the
  24.  * original release.
  25.  *
  26.  * IMPORTANT:
  27.  * This file is not identical to the original GNU release!
  28.  * You should have received this code as patch to the official
  29.  * GNU release.
  30.  *
  31.  * MORE IMPORTANT:
  32.  * This port comes with ABSOLUTELY NO WARRANTY.
  33.  *
  34.  * $Header: e:/gnu/fileutil/RCS/eaccess.c'v 1.3.0.2 90/06/29 00:46:42 tho Stable $
  35.  */
  36.  
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #ifdef _POSIX_SOURCE
  40. #include <unistd.h>
  41. #else
  42. #ifdef MSDOS
  43. #include "msd_pwd.h"
  44. #else /* not MSDOS */
  45. #include <sys/param.h>
  46. #endif /* not MSDOS */
  47. #endif
  48. #include <errno.h>
  49. #if defined(EACCES) && !defined(EACCESS)
  50. #define EACCESS EACCES
  51. #endif
  52.  
  53. #ifdef MSDOS
  54. #include <stdlib.h>
  55. extern  int eaccess (char *path, int mode);
  56. extern  int eaccess_stat (struct stat *statp, int mode);
  57. #endif /* MSDOS */
  58.  
  59. #ifndef _POSIX_SOURCE
  60. #define F_OK 0
  61. #define X_OK 1
  62. #define W_OK 2
  63. #define R_OK 4
  64. #endif
  65.  
  66. #ifndef STDC_HEADERS
  67. extern int errno;
  68. #endif
  69.  
  70. /* The user's effective user id. */
  71. static int euid;
  72.  
  73. /* The user's effective group id. */
  74. static int egid;
  75.  
  76. /* NGROUPS is defined in sys/param.h on systems with multiple groups. */
  77. #ifdef NGROUPS
  78. static int in_group ();
  79.  
  80. /* Array of group id's that the user is in. */
  81. static int groups[NGROUPS];
  82.  
  83. /* The number of valid elements in `groups'. */
  84. static int ngroups;
  85. #endif
  86.  
  87. /* Nonzero if the other static variables have valid values. */
  88. static int initialized = 0;
  89.  
  90. /* Return 0 if the user has permission of type MODE on file PATH;
  91.    otherwise, return -1 and set `errno' to EACCESS.
  92.    Like access, except that it uses the effective user and group
  93.    id's instead of the real ones, and it does not check for read-only
  94.    filesystem, text busy, etc. */
  95.  
  96. int
  97. eaccess (path, mode)
  98.      char *path;
  99.      int mode;
  100. {
  101.   struct stat stats;
  102.  
  103.   if (stat (path, &stats))
  104.     return -1;
  105.  
  106.   return eaccess_stat (&stats, mode);
  107. }
  108.  
  109. /* Like eaccess, except that a pointer to a filled-in stat structure
  110.    describing the file is provided instead of a filename. */
  111.  
  112. int
  113. eaccess_stat (statp, mode)
  114.      struct stat *statp;
  115.      int mode;
  116. {
  117.   int granted;
  118.  
  119.   mode &= (X_OK | W_OK | R_OK);    /* Clear any bogus bits. */
  120.  
  121.   if (mode == F_OK)
  122.     return 0;            /* The file exists. */
  123.  
  124.   if (initialized == 0)
  125.     {
  126.       initialized = 1;
  127.       euid = geteuid ();
  128.       egid = getegid ();
  129. #ifdef NGROUPS
  130.       ngroups = getgroups (NGROUPS, groups);
  131. #endif
  132.     }
  133.   
  134.   /* The super-user can read and write any file, and execute any file
  135.      that anyone can execute. */
  136.   if (euid == 0 && ((mode & X_OK) == 0 || (statp->st_mode & 0111)))
  137.     return 0;
  138.   if (euid == statp->st_uid)
  139.     granted = (statp->st_mode & (mode << 6)) >> 6;
  140.   else if (egid == statp->st_gid
  141. #ifdef NGROUPS
  142.        || in_group (statp->st_gid)
  143. #endif
  144.        )
  145.     granted = (statp->st_mode & (mode << 3)) >> 3;
  146.   else
  147.     granted = (statp->st_mode & mode);
  148.   if (granted == mode)
  149.     return 0;
  150.   errno = EACCESS;
  151.   return -1;
  152. }
  153.  
  154. #ifdef NGROUPS
  155. static int
  156. in_group (gid)
  157.      int gid;
  158. {
  159.   int i;
  160.  
  161.   for (i = 0; i < ngroups; i++)
  162.     if (gid == groups[i])
  163.       return 1;
  164.   return 0;
  165. }
  166. #endif
  167.  
  168. #ifdef TEST
  169. main (argc, argv)
  170.      char **argv;
  171. {
  172.   printf ("%d\n", eaccess (argv[1], atoi (argv[2])));
  173. }
  174. #endif
  175.